package top.heliming.youyou.cart.controller;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import top.heliming.youyou.cart.pojo.Cart;
import top.heliming.youyou.cart.service.CartService;

/**
 * description: //购物车
 *
 * @author: heliming
 * @date:2019/06/14 下午 10:22
 */
@Controller
public class CartController {

  @Autowired
  private CartService cartService;

  /**
   * 添加购物车
   *
   * @return
   */
  @PostMapping
  public ResponseEntity<Void> addCart(@RequestBody Cart cart) {
    this.cartService.addCart(cart);
    return ResponseEntity.ok().build();
  }

  /**
   * 查询购物车列表
   *
   * @return
   */
  @GetMapping
  public ResponseEntity<List<Cart>> queryCartList() {
    List<Cart> carts = this.cartService.queryCartList();
    if (carts == null) {
      return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null);
    }
    return ResponseEntity.ok(carts);
  }

  @PutMapping
  public ResponseEntity<Void> updateNum(@RequestParam("skuId") Long skuId,
      @RequestParam("num") Integer num) {
    this.cartService.updateNum(skuId, num);
    return ResponseEntity.ok().build();
  }

  @DeleteMapping("{skuId}")
  public ResponseEntity<Void> deleteCart(@PathVariable("skuId") String skuId) {
    this.cartService.deleteCart(skuId);
    return ResponseEntity.ok().build();
  }
}
